package com.kehao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kehao.mapper.StudentMapper;
import com.kehao.pojo.Student;
import com.kehao.vo.StudentRec;
import com.kehao.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;



    @Override
    public List<StudentRec> getAllStudent() {

        List<Student> allStudent = studentMapper.getAllStudent();

        //封装返回对象
        List<StudentRec> studentRecs = new ArrayList<>();
        allStudent.forEach(student -> studentRecs.add(new StudentRec(student.getId(), student.getUserName())));
//        System.out.println("studentRecs = " + studentRecs);
        return studentRecs;
    }

    @Override
    public StudentRec getOneStudent(Integer id) {

        Student oneStudent = studentMapper.getOneStudent(id);

        //封装返回值并返回
        return new StudentRec(oneStudent.getId(), oneStudent.getUserName());
    }

    @Override
    public void addStudent(Student student) {

        System.out.println("student = " + student);
        studentMapper.addStudent(student.getId(), student.getUserName(), student.getPassword());
    }

    @Override
    public void del(Integer id) {
        studentMapper.del(id);
    }

    @Override
    public void update(Student student) {
        studentMapper.update(student.getUserName(), student.getPassword(), student.getId());
    }
}
